
using UMC.Web;

namespace UMC.ITME.Activities
{

    [Apiumc("Account", "Secret", Desc = "终端安全", Weight = 0)]
    class SiteSecretActivity : UMC.Web.WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var key = this.AsyncDialog("Key", g => this.DialogValue("Secret"));
            var SharedSecret = this.AsyncDialog("PublicKey", r => UIDialog.Create("SignData"));
            var publicKeyData = Utility.FromBase64String(SharedSecret);
            var publicKey = NSec.Cryptography.PublicKey.Import(NSec.Cryptography.KeyAgreementAlgorithm.X25519, Utility.ToCurvePublicKey(publicKeyData), NSec.Cryptography.KeyBlobFormat.RawPublicKey);
            var sharedSecret = NSec.Cryptography.KeyAgreementAlgorithm.X25519.Agree(WebServlet.X25519, publicKey).Export(NSec.Cryptography.SharedSecretBlobFormat.RawSharedSecret);

            UMC.Data.DataFactory.Instance().Put(new Data.Entities.Session
            {
                SessionKey =new Net.StringValue( this.Context.Token.Device.ToString()),
                SignSecret = new Net.StringValue(sharedSecret),
                PublicKey = new Net.StringValue(publicKeyData)
            });
            this.Context.UIEvent(key, new ListItem(Utility.ToBase64String(sharedSecret)), true);
        }
    }
}
   